package com.xiaowu.controller;


import com.xiaowu.pojo.Blind;
import com.xiaowu.pojo.Blindgister;
import com.xiaowu.pojo.PageBean;
import com.xiaowu.pojo.Result;
import com.xiaowu.service.BlindService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.core.Local;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@CrossOrigin
@Slf4j
@RestController
public class BlindController {
    @Autowired //将Service层的对象注入到Controller层
    private BlindService bs;     //面向接口编程

    //查询所有数据
    @GetMapping("/blind")
    public Result list(){
        log.info("查询全部数据");
        List<Blind> blindList= bs.list();
        return Result.success(blindList);
    }

    //根据用户id删除
    @DeleteMapping("/blind/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("根据盲人id删除:{}",id);
        bs.delete(id);
        return Result.success();
    }


    //添加盲人用户
    @PostMapping("/addblind")
    public Result add(@RequestBody Blindgister blind){
        log.info("添加:{}",blind);
        bs.add(blind);
        return Result.success();
    }

    //分页查询
    @GetMapping("/blindpage")
    public Result list(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer pageSize){
        log.info("分页查询,参数:{}，{}",page,pageSize);
        PageBean pageBean=bs.page(page,pageSize);
        return Result.success(pageBean);
    }

    //分页条件查询
    @GetMapping("/blindselect")
    public Result lists(@RequestParam(defaultValue = "1") Integer page,
                        @RequestParam(defaultValue = "10") Integer pageSize,
                        String userId, String onLineStatus,
                        @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                        @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end ) throws Exception {
        log.info("分页查询,参数:{}，{},{},{},{},{}",page,pageSize,userId,onLineStatus,begin,end);
        PageBean pageBean=bs.pages(page,pageSize,userId,onLineStatus, begin, end);
        return Result.success(pageBean);
    }
}
